import type {
    ISystemLogin,
    ISystemResponse,
    IPermissionTree,
    IUserInfo,
    IPermission,
    IChannelModelField,
} from '@/model'
import type { IResponse, IResponseRows } from '@/api/http'
import { get, post, put, del } from '@/api/http'
import type { anyObj } from '@/types'

/**
 * 系统管理员相关接口
 * Author: Zane
 * Email: 873934580@qq.com
 * Date: 2023/5/4
 */

// 登录接口
export const login = async (data: ISystemLogin) => {
    return post<IResponse<ISystemResponse>>(`login`, data, { loading: true })
}

export const logout = async () => {
    return get<IResponse>(`logout`)
}

//
export const index = () => {}

// 获取授权信息
export const myPermission = () => {
    return get<IResponse<IPermissionTree[]>>(`my-permission`, {}, { loading: true })
}

// 修改用户信息
export const editUserInfo = (id: number, params: IUserInfo) => {
    return post<IResponse>(`/edit_user`, { ...params, id }, { loading: false, showSuccessMessage: false })
}

// 修改我的资料
export const myEditUserInfo = (params: anyObj) => {
    return put<IResponse>(`/my-system`, params)
}

// 修改我的密码
export const editPassword = (params: anyObj) => {
    return put<IResponse>(`/my-system-password`, params)
}

// 删除角色
export const delRole = (id: number) => {
    return del<IResponse>(`/roles/${id}`)
}

// 角色权限获取
export const getRolePermissions = (id: number) => {
    return get<IResponse>(`/roles-permission/${id}`)
}

// 角色权限设置
export const postRolePermissions = (id: number, params: any[]) => {
    return post<IResponse>(`/roles-permission/${id}`, {
        ids: params,
    })
}

// 新增权限管理
export const postPermissions = (parent_id: number, params: IPermission) => {
    return post<IResponse>(
        `/permissions`,
        {
            parent_id,
            ...params,
        },
        {
            loading: false,
            showSuccessMessage: false,
        },
    )
}

// 删除权限
export const delPermissions = (ids: any) => {
    return del<IResponse>(`/permissions`, {
        ids: ids,
    })
}

// 获取广告位列表
export const getAdvLists = () => {
    return get<IResponseRows>(`/ad-positions`)
}

// 根据模型ID获取模型字段
export const getModelFields = (id: number) => {
    return get<IResponse<IChannelModelField[]>>(`/model-fields/${id}`)
}

export const saveModelFields = (data: any, id: number) => {
    return post<IResponse>(`/model-fields/${id}`, data)
}

export const getConfig = () => {
    return get<IResponse<any[]>>(`configures`)
}
